<template>
  <div class="credit_list">
    <van-nav-bar
      title="我的学分"
      left-arrow
      :fixed="true"
      :placeholder="true"
      :z-index="999"
      :safe-area-inset-top="true"
      @click-left="goBack"
    />
    <div class="top">
      <div class="top-cont">
        <div class="total">
          <div>{{ totalCredit }}</div>
          <div>总学分</div>
        </div>
        <div class="line-list">
          <div class="line-item">
            <div>{{ creditToday }}</div>
            <div>今日学分</div>
          </div>
          <div class="line-item">
            <div>{{ ranking }}</div>
            <div>我的排名</div>
          </div>
        </div>
      </div>
    </div>
    <div class="credit_tab">
      <van-tabs v-model="creditType" @click="changeType" background="transparent" color="#5881db">
        <van-tab title="学员排行" name="rank"></van-tab>
        <van-tab title="学分详情" name="detail"></van-tab>
      </van-tabs>
    </div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <div class="course-list" v-if="creditType == 'detail'">
        <div class="nodata" v-if="!loading && courseList.length == 0">
          <img src="@static/img/nodata.png" alt="" />
        </div>
        <div class="course-item" v-for="(item, index) in courseList" :key="index" @click="gotoDetail(item)">
          <div class="course-cont">
            <div class="title">获得学分：{{ item.credit }}</div>
            <div class="time">
              所属任务类型：
              <span v-if="item.module == 'test'">考试</span>
              <span v-else-if="item.module == 'course'">课程</span>
              <span v-else-if="item.module == 'train'">培训</span>
            </div>
            <div class="sourse">所属任务名称：{{ item.targetTitle }}</div>
          </div>
        </div>
      </div>
      <div class="course-list" v-else>
        <div class="nodata" v-if="!loading && rankList.length == 0">
          <img src="@static/img/nodata.png" alt="" />
        </div>
        <div class="rank-item" v-for="(item, index) in rankList" :key="index">
          <div class="rank-cont">
            <div class="index" :class="{ active: index < 3 }">{{ index + 1 }}</div>
            <div class="avatar">
              <img :src="item.avatar" alt="" />
            </div>
            <div class="user-data">
              <div class="username">
                {{ item.userName }}
              </div>
            </div>
            <div class="credit">{{ item.totalCredit }}</div>
          </div>
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>

export default {
  name: 'creditList',
  components: {},
  data() {
    return {
      totalCredit: '', // 总学分
      ranking: 0, // 排名
      creditToday: 0, //今日学分
      courseList: [],
      rankList: [],
      loading: false,
      finished: false,
      categoryList: [], // 分类列表
      isShow: false,
      page: 1,
      pageSize: 10,
      categoryId: '', // 分类id
      type: this.$route.query.type || 'all', // 标识我的选修还是必修还是课程中心
      params: {},
      active: 1,
      creditType: 'rank'
    }
  },
  computed: {},
  mounted() {
    this.getCreditInfo()
  },
  methods: {
    changeType(name, title) {
      this.creditType = name
      this.page = 1
      this.courseList = []
      this.rankList = []
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    goBack() {
      this.$router.go(-1)
    },
    getCreditInfo() {
      this.totalCredit = 0
          this.ranking = 0
          this.creditToday = 0
    },
    // 获取课程列表
    getCreditList() {
      this.courseList = []
      this.loading = false
      this.finished = true

    },
    getCreditRank() {
      this.rankList = []
      this.loading = false
      this.finished = true

    },
    // 确认搜索
    confirmSearch() {
      this.courseList = []
      this.rankList = []
      this.page = 1
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    // 取消搜索
    cancelSearch() {
      this.searchValue = ''
      this.courseList = []
      this.rankList = []
      this.page = 1
      this.finished = false
      this.loading = true
      this.onLoad()
    },
    // 前往学习
    gotoDetail(item) {
      if (item.approveStatus == 'notApprove') {
        this.$router.push({
          name: 'homeworkShenpi',
          query: {
            uuid: item.uuid
          }
        })
      } else {
        this.$router.push({
          name: 'shenpiPreview',
          query: {
            uuid: item.uuid
          }
        })
      }
    },
    onLoad() {
      if (this.creditType == 'rank') {
        this.getCreditRank()
      } else {
        this.getCreditList()
      }
    }
  }
}
</script>

<style scoped lang="less">
.credit_list {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
  background-color: #f5f5f5;

  .top {
    width: 100%;
    padding: 10px 10px 5px;
    box-sizing: border-box;
    font-size: 14px;
    color: #fff;

    .top-cont {
      width: 100%;
      height: 150px;
      background: url('@static/img/head-bg.png') no-repeat #fff;
      padding: 20px;
      box-sizing: border-box;
      border-radius: 6px;

      .total {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        > div {
          &:nth-child(1) {
            font-size: 20px;
            font-weight: bold;
          }

          &:nth-child(2) {
            font-size: 14px;
          }
        }
      }

      .line-list {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-around;
        margin-top: 20px;

        .line-item {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;

          > div {
            &:nth-child(1) {
              font-size: 20px;
              font-weight: bold;
            }

            &:nth-child(2) {
              font-size: 14px;
            }
          }
        }
      }
    }
  }

  .credit_tab {
    width: 100%;
    background: transparent;
  }

  .course-list {
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;

    .nodata {
      width: 100%;
      height: 230px;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      align-items: center;

      img {
        width: 160px;
      }

      div {
        line-height: 50px;
        font-size: 14px;
        color: #999;
      }
    }

    .rank-item {
      width: 100%;
      margin-top: 10px;

      .rank-cont {
        width: 100%;
        background-color: #fff;
        border-radius: 6px;
        padding: 6px 10px 10px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        .index {
          flex-shrink: 0;
          width: 30px;
          height: 30px;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-right: 10px;
          background: #eee;
          color: #333;
          font-size: 14px;
          border-radius: 50%;
          &.active {
            border-radius: 0;
            background: url(~@static/img/rank.png);
            background-size: 100% 100%;
            color: #fff;
          }
        }
        .avatar {
          flex-shrink: 0;
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .user-data {
          width: 220px;
          overflow: hidden;
          padding: 0 10px;
          box-sizing: border-box;
          font-size: 14px;
        }
        .credit {
          flex-shrink: 0;
          margin-left: 10px;
          color: #ef962f;
          font-size: 14px;
          font-weight: bold;
        }
      }
    }

    .course-item {
      width: 100%;
      margin-top: 10px;

      .course-cont {
        width: 100%;
        background-color: #fff;
        border-radius: 6px;
        padding: 6px 10px 10px;
        box-sizing: border-box;

        .title {
          font-size: 14px;
          color: #333;
          font-weight: bold;
          margin-bottom: 10px;
        }

        .sourse {
          font-size: 12px;
          color: #333;
          margin-bottom: 5px;
        }

        .time {
          font-size: 12px;
          color: #333;
          margin-bottom: 10px;
        }

        .bottom {
          font-size: 12px;
          color: #333;
          width: 100%;
          height: 40px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          border-top: 1px solid #eee;

          .btn {
            width: 60px;
            height: 25px;
            background: #5881db;
            text-align: center;
            line-height: 25px;
            color: #fff;
            border-radius: 4px;
            font-size: 12px;
          }

          .status {
            color: #5881db;
            font-size: 12px;
            white-space: nowrap;
          }

          .submitUser {
            display: flex;
            align-items: center;

            span {
              flex-shrink: 0;
            }
          }
        }
      }
    }
  }

  .category-box {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transform: scaleX(0);
    -webkit-transform: scaleX(0);
    transition: all 0.2s;
    -webkit-transition: all 0.2s;
    transform-origin: 0% 50%;

    .category-list {
      position: absolute;
      top: 0;
      left: 0;
      width: 80%;
      height: 100%;
      background-color: #fff;
    }
  }

  .category-show {
    transform: scaleX(1);
    -webkit-transform: scaleX(1);
  }
}
</style>
